package com.sinothk.gpserver.modules.biz.controller;

import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.biz.entity.RoomEntity;
import com.sinothk.gpserver.modules.biz.entity.RoomVo;
import com.sinothk.gpserver.modules.biz.service.IRoomService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 1. 房间相关
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-08-30
 */
@Api(tags = "房间相关")
@RequestMapping("/room")
@ResponseResult()
@RestController
public class RoomController extends BaseController {

    @Resource
    private IRoomService roomService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<RoomEntity> page(@RequestBody PageVo<RoomEntity> pageVo) {
        return roomService.page(pageVo);
    }

    @ApiOperation(value = "分页列表-用户加入的俱乐部的房间", notes = "分页列表-用户加入的俱乐部的房间")
    @PostMapping("/pageForGroupMember")
    public PageData<RoomEntity> pageForGroupMember(@RequestBody PageVo<RoomEntity> pageVo) {
        return roomService.pageForGroupMember(pageVo);
    }

    @ApiOperation(value = "分页列表-用户加入的俱乐部的房间-7张", notes = "分页列表-用户加入的俱乐部的房间-7张")
    @PostMapping("/page7CardsForGroupMember")
    public PageData<RoomEntity> page7CardsForGroupMember(@RequestBody PageVo<RoomEntity> pageVo) {
        return roomService.page7CardsForGroupMember(pageVo);
    }

    @ApiOperation(value = "分页列表-俱乐部的房间", notes = "分页列表-俱乐部的房间")
    @PostMapping("/pageForGroupId")
    public PageData<RoomEntity> pageForGroupId(@RequestBody PageVo<RoomEntity> pageVo) {
        return roomService.pageForGroupId(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public RoomEntity retrieve(@ApiParam("id") @PathVariable String id) {
        return roomService.getById(id);
    }

    @ApiOperation(value = "详情-通过RoomNum查询", notes = "详情-通过RoomNum查询")
    @GetMapping("/retrieveByRoomNum")
    public RoomEntity retrieveByRoomNum(@ApiParam("房间号") @RequestParam("roomNum") String roomNum) {
        return roomService.retrieveByRoomNum(roomNum);
    }

    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping(value = "/add")
    public RoomEntity add(@RequestBody RoomVo roomVo) {
        return roomService.create(roomVo);
    }

    @ApiOperation(value = "房间复制", notes = "房间复制")
    @PostMapping(value = "/copy")
    public String copy(@ApiParam("房间号") @RequestParam("roomNum") String roomNum) {
        roomService.copy(roomNum);
        return "房间复制成功";
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public RoomEntity update(@RequestBody RoomEntity roomEntity) {
        if (StringUtil.isEmpty(roomEntity.getId())) {
            throw new BizException("id不能为空");
        }
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        roomEntity.setUpdateBy(login.getUserName());
        roomService.updateById(roomEntity);
        return roomService.getById(roomEntity.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody RoomEntity roomEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (StringUtil.isEmpty(roomEntity.getId())) {
            roomEntity.setCreateBy(login.getAccount());
            roomService.save(roomEntity);
            return "新增成功";
        } else {
            roomEntity.setUpdateBy(login.getUserName());
            roomService.updateById(roomEntity);
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        roomService.removeById(id);
        return "已逻辑删除";
    }
}
